package com.beaven.imclient.fragment;

import android.app.ProgressDialog;
import android.content.Context;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.beaven.imclient.R;
import com.beaven.imclient.activity.ChatActivity;
import com.beaven.imclient.activity.SettingActivity;
import com.beaven.imclient.base.BaseFragment;
import com.beaven.imclient.utils.ToastUtil;
import com.beaven.model.data.helper.DbHelper;
import com.beaven.model.data.helper.GroupHelper;
import com.beaven.model.model.api.RspModel;
import com.beaven.model.model.api.group.UpdateGroupModel;
import com.beaven.model.model.card.GroupCard;
import com.beaven.model.model.db.Group;
import com.beaven.model.model.sp.Account;
import rx.Subscriber;
import rx.subscriptions.CompositeSubscription;

/**
 * A simple {@link Fragment} subclass.
 */
public class GroupUpdateFragment extends BaseFragment {

  public static GroupUpdateFragment newInstance() {
    return new GroupUpdateFragment();
  }

  private String groupId;
  private ProgressDialog dialog;
  private CompositeSubscription subscription;

  @Override protected int getLayoutId() {
    return R.layout.fragment_group;
  }

  @Override public void onAttach(Context context) {
    super.onAttach(context);
    SettingActivity activity = (SettingActivity) context;
    groupId = activity.getIntent().getStringExtra(ChatActivity.KEY_RECEIVER_ID);
  }

  @Override protected void initView(View rootView) {
    super.initView(rootView);
    Button bt_create = (Button) rootView.findViewById(R.id.bt_create);
    final EditText edit_group_name = (EditText) rootView.findViewById(R.id.edit_group_name);
    bt_create.setText(R.string.save);
    bt_create.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        String edit_name = edit_group_name.getText().toString().trim();
        if (TextUtils.isEmpty(edit_name)) {
          return;
        }
        UpdateGroupModel model = new UpdateGroupModel(edit_name, groupId);
        showLoading();
        subscription.add(
            GroupHelper.updateGroup(model).subscribe(new Subscriber<RspModel<GroupCard>>() {
              @Override public void onCompleted() {
                hideLoading();
              }

              @Override public void onError(Throwable e) {
                ToastUtil.show(R.string.data_network_error);
                hideLoading();
              }

              @Override public void onNext(RspModel<GroupCard> groupCardRspModel) {
                if (groupCardRspModel.isSuccess()) {
                  DbHelper.instance()
                      .saveModel(Group.class,
                          groupCardRspModel.getResult().build(Account.getUser()));
                  ToastUtil.show("修改成功");
                } else {
                  ToastUtil.show("修改失败");
                }
              }
            }));
      }
    });
  }

  @Override protected void initData() {
    super.initData();
    subscription = new CompositeSubscription();
  }

  @Override public void onPause() {
    super.onPause();
    hideLoading();
    subscription.clear();
  }

  public void showLoading() {
    dialog = ProgressDialog.show(getActivity(), "", getString(R.string.load_now));
  }

  public void hideLoading() {
    if (dialog != null) {
      dialog.dismiss();
    }
  }
}
